import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

months = ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"]
monthly_sales = [2354, 1902, 3524, 2698, 2896, 2563, 3156, 2896, 3621, 2635, 2963, 2789]
quarterly_sales = [7780, 7780, 7780, 8157, 8157, 8157, 9673, 9673, 9673, 8387, 8387, 8387]

x_groups = [0, 1, 2, 4, 5, 6, 8, 9, 10, 12, 13, 14]
group_labels = ["Q1", "Q2", "Q3", "Q4"]

quarter_colors = ['#4a4a8c', '#8c4a4a', '#4a8c4a', '#8c4a8c']

fig, ax = plt.subplots(figsize=(12, 7))
bar_width = 0.4
spacing = 1.5

for i, (x, sale) in enumerate(zip(x_groups, monthly_sales)):
    ax.bar(x, sale, width=bar_width, color=['#6a87ff', '#6a87ff', '#6a87ff', '#ff8b4b', '#ff8b4b', '#ff8b4b', '#a87332', '#a87332', '#a87332', '#b84d9b', '#b84d9b', '#b84d9b'][i])
    ax.text(x, sale + 100, str(sale), ha='center', va='bottom', color='white', fontsize=10)

for i in range(0, len(x_groups), 3):
    group_x = np.mean(x_groups[i:i+3])
    ax.bar(group_x, quarterly_sales[i], width=bar_width * 3 + spacing, color=quarter_colors[i // 3], alpha=0.6)
    ax.text(group_x, quarterly_sales[i] + 200, str(quarterly_sales[i]), ha='center', va='bottom', color='black', fontsize=12)

ax.set_xticks([np.mean(x_groups[i:i+3]) for i in range(0, len(x_groups), 3)])
ax.set_xticklabels(group_labels)
ax.set_xlabel('季度')
ax.set_ylabel('销量')
ax.set_title('2021年各月化妆品销量走势\n2021年第三季度销量最多9673，9月单月销量最大3621')

plt.figtext(0.1, -0.05, '*注：数据源于公司销售系统，统计日期截至2021.12.31', ha="left", fontsize=10, color="grey")

plt.tight_layout()

#plt.savefig('D:/python charm/01/可视化/第二章/图片/复合柱形图.png')

plt.show()
